package com.fosss.band.service.impl;

import com.fosss.band.entity.Comment;
import com.fosss.band.entity.User;
import com.fosss.band.entity.vo.CommentVo;
import com.fosss.band.mapper.CommentMapper;
import com.fosss.band.mapper.UserMapper;
import com.fosss.band.service.CommentService;
import com.fosss.band.utils.Result;
import com.fosss.band.utils.SensitiveFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: fosss
 * Date: 2023/12/27
 * Time: 20:38
 * Description:
 */
@Service
public class CommentServiceImpl implements CommentService {

    @Resource
    private CommentMapper commentMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private SensitiveFilter sensitiveFilter;


    /**
     * 获取专辑评论
     */
    @Override
    public Result selectAll(Integer albumId, String order) {
        // 根据专辑id获取所有对专辑的评论
        List<Comment> oneList = commentMapper.getByAlbumId(albumId, order);
        List<CommentVo> res = oneList.stream().map(comment -> {
            CommentVo commentVo = new CommentVo();
            //查询用户信息
            User user = userMapper.selectById(comment.getFanId());
            //设置内容
            commentVo.setId(comment.getId());
            commentVo.setUsername(user.getUsername());
            commentVo.setAvatar(user.getAvatar());
            commentVo.setContent(comment.getDescription());
            commentVo.setCreateTime(comment.getCreateTime());
            return commentVo;
        }).collect(Collectors.toList());
        return Result.ok().data("rows", res);
    }

    /**
     * 添加评论
     */
    @Override
    public Result add(Comment comment) {
        comment.setCreateTime(LocalDateTime.now());
        String content = sensitiveFilter.filter(comment.getDescription());
        comment.setDescription(content);
        commentMapper.insert(comment);
        return Result.ok();
    }
}
